/**
 * Created by liguangsong on 16/8/3.
 */
import React, { PropTypes } from 'react'
import TitleView from './titleView.js'
import AverageData from './averageData.js'
import OneGameData from './oneGameData.js'
import SelectDateView from './selectDateView.js'
import { clickArrow, refreshIndex } from '../../action/index.js'
import { connect } from 'react-redux'
var getJson = require('../../assist/getJson');

var PlayerInfo = React.createClass( {
    getInitialState() {
        var id = this.props.params.id;
        var urlPlayer = "commonplayerinfo?PlayerID=" + id;
        getJson(urlPlayer, function (result) {
            this.setState( {
                playerAverageData: result
            })
        }.bind(this));

        var urlAllPlayer = "playergamelog?PlayerID=" + id + "&Season=2015-16&SeasonType=Regular%20Season";
        getJson(urlAllPlayer, function(result) {
            this.setState( {
                playerAllGames: result["resultSets"][0]['rowSet']
            });
        }.bind(this));


        this.props.dispatch(refreshIndex());


        return {
            playerAverageData: {},
            playerAllGames: []
        };
    },

    getPlayerNameAndNumber() {
        return {
            id: '0',
            name: '',
            number: '',
            team: ''
        }
    },

    getAverageInfo() {
        return {
            point: 'null',
            assist: 'null',
            rebound: 'null'
        }
    },

    refreshAverageData() {
        var nameAndNumber = this.getPlayerNameAndNumber();
        var averageInfo = this.getAverageInfo();
        //console.log(this.state.playerAverageData);
        if (!$.isEmptyObject(this.state.playerAverageData)) {
            nameAndNumber.team = this.state.playerAverageData['resultSets'][0]['rowSet'][0][17];
            nameAndNumber.id = this.state.playerAverageData['parameters'][0]['PlayerID'];
            nameAndNumber.name = this.state.playerAverageData['resultSets'][0]['rowSet'][0][3];
            nameAndNumber.number = this.state.playerAverageData['resultSets'][0]['rowSet'][0][13];
            averageInfo.point = this.state.playerAverageData['resultSets'][1]['rowSet'][0][3].toFixed(1);
            averageInfo.assist = this.state.playerAverageData['resultSets'][1]['rowSet'][0][4].toFixed(1);
            averageInfo.rebound = this.state.playerAverageData['resultSets'][1]['rowSet'][0][5].toFixed(1);
        }

        return {
            nameAndNumber: nameAndNumber,
            average: averageInfo
        }
    },

    getInitialData() {
        return {
            points: 0,
            assist: 0,
            rebounds: 0,
            steals: 0,
            blocks: 0,
            turnovers: 0,
            minutes: 0,
            dateStr: 'null'
        }
    },

    getGameDataCount() {
        var count = this.props.index;
        if (count < 0) {
            count = 0;
        }
        if (count >= this.state.playerAllGames.length ) {
            count = this.state.playerAllGames.length - 1;
        }

        return count;
    },

    getGameData(data) {

        var todayGame = this.state.playerAllGames[this.getGameDataCount()];

        data.points = todayGame[24];
        data.assist = todayGame[19];
        data.rebounds = todayGame[18];
        data.steals = todayGame[20];
        data.blocks = todayGame[21];
        data.turnovers = todayGame[22];
        data.minutes = todayGame[6];

        var date = new Date(todayGame[3]);
        data.dateStr = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();

        return data;
    },

    refreshGameData() {

        var data = this.getInitialData();

        //console.log(this.state.playerAllGames.length);

        if (this.state.playerAllGames.length != 0) {
            data = this.getGameData(data);
        }
        return data;
    },

    render() {

        const { dispatch, index } = this.props;

        var info = this.refreshAverageData();
        var gameInfo = this.refreshGameData();
        //console.log(gameInfo);
        //console.log('------------');
        return (
            <div>
                <TitleView backColor={this.props.params.backColor}  playerId={info.nameAndNumber.id} playerName={this.props.params.playerName} playerNumber={info.nameAndNumber.number}/>
                <AverageData point={info.average.point} assist={info.average.assist} rebound={info.average.rebound}/>
                <OneGameData point={gameInfo.points} assist={gameInfo.assist} rebound={gameInfo.rebounds} steal={gameInfo.steals}
                    block={gameInfo.blocks} turnover={gameInfo.turnovers} minute={gameInfo.minutes}
                    />
                <SelectDateView date={gameInfo.dateStr}  length={this.state.playerAllGames.length} index={index} team={info.nameAndNumber.team} clickArrow={ arrow => dispatch(clickArrow(arrow)) } />
            </div>
        )
    }
});

PlayerInfo.propTypes = {
    index: PropTypes.number.isRequired
};

function select(state) {
    return {
        index: state.clickArrow.index
    }
}

export default connect(select)(PlayerInfo);
//export default PlayerInfo;

